package com.example.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2023/1/9
 * @time: 22:07
 */
@Slf4j
@Service
public class SendMailService {
    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${spring.mail.mail-to}")
    private String to;

    @Value("${spring.mail.mail-title}")
    private String title;

    public void sendMailToManager(String content) {
        if (!StringUtils.hasText(from)||!StringUtils.hasText(to)||!StringUtils.hasText(title)){
            log.warn("请在application.yml中完善邮箱配置信息...");
            return;
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(title);
        message.setText(content);
        javaMailSender.send(message);
    }
}
